package com.zone.mybatis.reflection.property;

import java.util.Locale;

/**
 * @author: zongzi
 * @description: TODO
 * @date: 2024/1/8
 */
public class PropertyNamer {

    /**
     * 方法名转属性名
     *
     * @param methodName
     * @return
     */
    public static String methodToProperty(String methodName) {
        if (methodName.startsWith("is")) {
            methodName = methodName.substring(2);
        } else if (methodName.startsWith("get") || methodName.startsWith("set")) {
            methodName = methodName.substring(3);
        } else {
            throw new RuntimeException("未识别的methodName: " + methodName);
        }
        // getter命名规范：userName:getUserName、uName:getuName、URL:getURL、flag(boolean):isFlag
        // 如果第二个字符不为大写，首个字符转为小写
        if (methodName.length() == 1 || (methodName.length() > 1 && !Character.isUpperCase(methodName.charAt(1)))) {
            methodName = methodName.substring(0, 1).toLowerCase(Locale.ENGLISH) + methodName.substring(1);
        }
        return methodName;
    }
}
